package com.ctg.behavior.calc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.calculator.CtEventCalculator;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.event.CtEventCalcRequest;
import com.ctg.behavior.common.event.CtEventCalcResult;
import com.ctg.behavior.common.event.CtEventVSCalcResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/events")
public class CtEventReportController {

	@Autowired
	private CtEventCalculator eventCalculator;

	@PostMapping("/report")
	public CtEventCalcResult calcReport(@RequestBody CtEventCalcRequest request) throws Exception {

		CtEventCalcResult calcResult = new CtEventCalcResult();

		log.info("body={}", JSON.toJSONString(request.getReq()));

		CtCalcResult result = eventCalculator.doCalculator(request.getReq());
		log.info("calcResult={}", JSON.toJSONString(calcResult));
		if (!(result instanceof CtEventCalcResult)) {
			log.error("calcResult class={}", calcResult.getClass());
			log.error("calcResult={}", JSON.toJSONString(calcResult));
			return calcResult;
		}
		calcResult = (CtEventCalcResult) result;

		return calcResult;

	}

	@PostMapping("/reportvs")
	public CtEventVSCalcResult calcReportVS(@RequestBody CtEventCalcRequest request) throws Exception {

		CtEventVSCalcResult calcResult = new CtEventVSCalcResult();

		log.info("body={}", JSON.toJSONString(request.getReq()));

		{
			log.info("VS-L start...");
			CtCalcResult result = eventCalculator.doCalculator(request.getReq());
			log.info("calcResult={}", JSON.toJSONString(calcResult));
			if (!(result instanceof CtEventCalcResult)) {
				log.error("calcResult class={}", calcResult.getClass());
				log.error("calcResult={}", JSON.toJSONString(calcResult));
				return calcResult;
			}
			CtEventCalcResult vslResult = (CtEventCalcResult) result;
			calcResult.setSuccess(vslResult.getSuccess());
			calcResult.setExtInfo(vslResult.getExtInfo());
			if (!calcResult.getSuccess()) {
				calcResult.setErrorCode(vslResult.getErrorCode());
				calcResult.setErrorMessage(vslResult.getErrorMessage());
				return calcResult;
			}
			calcResult.getValue().add(vslResult.getValue());
			log.info("VS-L end...");
		}

		{
			request.getReq().setFromDate(request.getReq().getCompareFromDate());
			request.getReq().setToDate(request.getReq().getCompareToDate());
		}

		{
			log.info("VS-R start...");
			CtCalcResult result = eventCalculator.doCalculator(request.getReq());
			log.info("calcResult={}", JSON.toJSONString(calcResult));
			if (!(result instanceof CtEventCalcResult)) {
				log.error("calcResult class={}", calcResult.getClass());
				log.error("calcResult={}", JSON.toJSONString(calcResult));
				return calcResult;
			}
			CtEventCalcResult vslResult = (CtEventCalcResult) result;
			calcResult.setSuccess(vslResult.getSuccess());
			calcResult.setExtInfo(vslResult.getExtInfo());
			if (!calcResult.getSuccess()) {
				calcResult.setErrorCode(vslResult.getErrorCode());
				calcResult.setErrorMessage(vslResult.getErrorMessage());
				return calcResult;
			}
			calcResult.getValue().add(vslResult.getValue());
			log.info("VS-R end...");
		}

		return calcResult;

	}

}
